package com.liuym.classes.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liuym.classes.repository.model.bo.ClassScoreBO;
import com.liuym.classes.service.ClassScoreService;
import com.liuym.common.dubbo.classes.api.ClassScoreDubboService;
import com.liuym.common.dubbo.classes.dto.ClassScoreDTO;
import com.liuym.common.exception.ClassException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Liuym
 * @date 2018/12/12 17:34
 * @describe：
 */
@Service(version = "0.0.1")
public class ClassScoreDubboServiceImpl implements ClassScoreDubboService {

    @Autowired
    private ClassScoreService classScoreService;

    @Override
    public boolean addClassScore(String classScoreTableName, String userName, ClassScoreDTO classScoreDTO) throws ClassException {
        ClassScoreBO classScoreBO = BeanMapper.map(classScoreDTO, ClassScoreBO.class);
        return classScoreService.addClassScore(classScoreTableName, userName, classScoreBO);
    }

    @Override
    public Map<String, Object> addClassScoreList(String classScoreTableName, String userName, List<ClassScoreDTO> classScoreDTOList) throws ClassException {
        List<ClassScoreBO> classScoreBOList =
                classScoreDTOList
                        .stream()
                        .map(s -> BeanMapper.map(s, ClassScoreBO.class))
                        .collect(Collectors.toList());
        return classScoreService.addClassScoreBatch(classScoreTableName, userName, classScoreBOList);
    }

    @Override
    public List<ClassScoreDTO> getClassScoreByClassNumber(String classScoreTableName, long classNumber) throws ClassException {
        List<ClassScoreBO> classScoreBOList = classScoreService.getClassScoreByNumber(classScoreTableName, classNumber);
        if (classScoreBOList != null && classScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<ClassScoreDTO> classScoreDTOList =
                    classScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassScoreDTO.class))
                            .collect(Collectors.toList());
            return classScoreDTOList;
        }
        return null;
    }

    @Override
    public ClassScoreDTO getClassScoreById(String classScoreTableName, int id) throws ClassException {
        return BeanMapper.map(classScoreService.getClassScoreById(classScoreTableName, id), ClassScoreDTO.class);
    }

    @Override
    public void updateClassScore(String classScoreTableName, ClassScoreDTO classScoreDTO,Integer oldScore) throws ClassException {
        classScoreService.updateClassScore(classScoreTableName, BeanMapper.map(classScoreDTO, ClassScoreBO.class),oldScore);
    }

    @Override
    public void deleteClassScore(String classScoreTableName, int id) throws ClassException {
        classScoreService.deleteClassScore(classScoreTableName, id);
    }

    @Override
    public List<ClassScoreDTO> getClassScoreByTime(String classScoreTableName, Date startTime, Date endTime) throws ClassException {

        List<ClassScoreBO> classScoreBOList = classScoreService.getClassScoreByTime(classScoreTableName, startTime, endTime);
        if (classScoreBOList != null && classScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<ClassScoreDTO> classScoreDTOList =
                    classScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassScoreDTO.class))
                            .collect(Collectors.toList());
            return classScoreDTOList;
        }
        return null;
    }

    @Override
    public List<ClassScoreDTO> getClassScoreByAdmin(String classScoreTableName, String userName, Date startTime, Date endTime) throws ClassException {
        List<ClassScoreBO> classScoreBOList = classScoreService.getClassScoreByAdmin(classScoreTableName, userName, startTime, endTime);
        if (classScoreBOList != null && classScoreBOList.size() > 0) {
            // jdk1.8实现list类型转换
            List<ClassScoreDTO> classScoreDTOList =
                    classScoreBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassScoreDTO.class))
                            .collect(Collectors.toList());
            return classScoreDTOList;
        }
        return null;
    }

    @Override
    public int addClassScoreTable(String classScoreTableName) throws ClassException {
        return classScoreService.addClassScoreTable(classScoreTableName);
    }
}
